package com.byxy.ssm.demo.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.byxy.ssm.demo.entity.User;
import com.byxy.ssm.demo.service.UserService;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/")

@Slf4j //lombok
public class UserController {

	@Autowired
	private UserService userSerice;

	@GetMapping("toLoginPage.do")
	public String login() {
		return "login";
	}

	@PostMapping("userLogin.do")
	public String doLogin(String userName, String userPassword, String tipId, HttpSession session) {
		// User 使用VO类代替，防止密码
		User u = userSerice.getByNameAndPassword(userName, userPassword);
		log.info("userName = " +userName );
		log.info("userPassword = " +userPassword );
		log.info("user = " +u );
		if (u == null) {
			session.setAttribute("error", "用户名与密码不正确");
			return "redirect:toLoginPage.do";
		}
			
		else {
			u.setUserPassword("");
			session.setAttribute("USER", u);
			session.removeAttribute("error");
			return "redirect:toMainPage.do";
		}

	}

	
	@GetMapping("toSignUpPage.do")
	public String reg() {
		return "signUp";
	}
	
	@PostMapping("userSignUp.do")
	public String doReg(String user_name,String user_nick,String user_password,Integer user_type) {
		log.info("user_name = "+ user_name);
		log.info("user_nick = "+ user_nick);
		log.info("user_password = "+ user_password);
		log.info("user_type = "+ user_type);
		// 第5步
		userSerice.save(user_name,user_nick,user_password,user_type);
		return "redirect:toLoginPage.do";
	}
}
